package com.xiaomengkuan.common.util;

import com.xiaomengkuan.common.entity.vo.PageVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-01 9:28 下午, File Encoding: utf8 --
 */
@Data
@Slf4j
public class R {
    private int code;
    private String msg;
    private Map<String, Object> data;

    protected void setCode(int code){
        this.code = code;
    }

    protected void setData(Map<String,Object> data){
        this.data = data;
    }

    protected void setData(String key, Object value) {
        this.data.put(key, value);
    }

    public static R ok(){
        R r = new R();
        r.data = new HashMap<>();
        r.msg = "success";
        r.setCode(200);
        return r;
    }

    public static R err(){
        R r = new R();
        r.msg = "upset";
        r.data = new HashMap<>();
        r.setCode(5000);
        return r;
    }

    public R detail(String message){
        setMsg(message);
        return this;
    }

    public R put(Map<String, Object> data){
        setData(data);
        return this;
    }

    public R putList(List<? extends Object> list) {
        this.put("list", list);
        return this;
    }

    public R putItem(Object item) {
        this.put("item", item);
        return this;
    }


    public R put(String key, Object value) {
        setData(key, value);
        return this;
    }

    public R page(PageVo page) {
        put("page", page);
        return this;
    }


    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", data=" + data +
                '}';
    }
}
